package com.cdream.basic.algorithm.leetcode.tree;

public class Question114 {
    public void flatten(TreeNode root) {
        if (root == null) {
            return;
        }
        TreeNode head = new TreeNode();
        searchTree(root, head);
        root.left = null;
        root.right = head.right.right;
    }

    private TreeNode searchTree(TreeNode root, TreeNode head) {
        if (root == null) {
            return head;
        }
        head.right = new TreeNode(root.val);
        TreeNode treeNode = searchTree(root.left, head.right);
        return searchTree(root.right, treeNode);
    }

    public static void main(String[] args) {
        TreeNode t3 = new TreeNode(3);
        TreeNode t4 = new TreeNode(4);
        TreeNode t2 = new TreeNode(2, t3, t4);
        TreeNode t6 = new TreeNode(6);
        TreeNode t5 = new TreeNode(5, null, t6);
        TreeNode t1 = new TreeNode(1, t2, t5);

        new Question114().flatten(t1);

        for (int i = 1; i < 1; i++) {
            System.out.println(i);
        }
    }
}
